import { Component, OnInit, ViewEncapsulation } from '@angular/core';
import { Router } from '@angular/router';
import { HttpService } from './service/http.service';
import { Observable } from 'rxjs/Observable';

@Component({
  selector: 'app-root',
  templateUrl: './app.component.html',
  styleUrls: ['./app.component.css'],
  encapsulation: ViewEncapsulation.None
})

export class AppComponent implements OnInit {

  constructor(private router: Router, private httpservice: HttpService) { }
  //根据openid登录获取就诊人列表字段
  patientlisturl = 'patient!getBindPatientListByWX';
  openidurl = 'weixin!getToken';

  status: string;

  ngOnInit() {
    this.getopenidurl();
  }

  getopenidurl() {
<<<<<<< HEAD
    // this.getbrid();
    // if (this.getwechatcode() == null) {
    //   let appid = "wx42d7cd393e02dd2f";
    //   let backurl = encodeURI("http://yuyue.cqjydata.com");
    //   window.location.href = "https://open.weixin.qq.com/connect/oauth2/authorize?appid=" + appid + "&redirect_uri=" + backurl + "&response_type=code&scope=snsapi_base&state=STATE#wechat_redirect";
    // } else {
    //   let data = "{code:'" + this.getwechatcode() + "'}";
    //   this.httpservice.httpPost(this.openidurl, data).subscribe(res => {
    //     sessionStorage.setItem('openid', res.openid);
    //     this.getwechatuserinfo(res.openid);
    //     // sessionStorage.setItem('openid', "o9j9uwum7wuPlxSshZ6d7Ww2O_bI");
    //     // this.getwechatuserinfo("o9j9uwum7wuPlxSshZ6d7Ww2O_bI");
    //   });
    // }
    sessionStorage.setItem('brid', "4541194");
    this.router.navigate(['/outpatientpay/scanoutpatientpay']);
=======
    this.getbrid();
    if (this.getwechatcode() == null) {
      let appid = "wx42d7cd393e02dd2f";
      let backurl = encodeURI("http://yuyue.cqjydata.com");
      window.location.href = "https://open.weixin.qq.com/connect/oauth2/authorize?appid=" + appid + "&redirect_uri=" + backurl + "&response_type=code&scope=snsapi_base&state=STATE#wechat_redirect";
    } else {
      let data = "{code:'" + this.getwechatcode() + "'}";
      this.httpservice.httpPost(this.openidurl, data).subscribe(res => {
        sessionStorage.setItem('openid', res.openid);
        this.getwechatuserinfo(res.openid);
        // sessionStorage.setItem('openid', "o9j9uwum7wuPlxSshZ6d7Ww2O_bI");
        // this.getwechatuserinfo("o9j9uwum7wuPlxSshZ6d7Ww2O_bI");
      });
    }
    // sessionStorage.setItem('brid', "4543819");
    // this.router.navigate(['/outpatientpay/scanoutpatientpay']);
>>>>>>> a8bd0c6d08c8d5babb7ba313a8ca692406ffd5d2
  }

  //微信用户登录
  getwechatuserinfo(openid) {
    let data = "{wx_openid:'" + openid + "'}";
    console.log(this.patientlisturl + ':' + data);
    this.httpservice.httpPost(this.patientlisturl, data).subscribe(res => {
      if (res.data != null) {
        console.log(res.data);
        sessionStorage.setItem('patientlist', JSON.stringify(res.data));
      }
    });
  }

  //获取code
  getwechatcode() {
    var url = document.location.toString();
    var arrObj = url.split("?");
    if (arrObj.length > 1) {
      var arrPara = arrObj[1].split("&");
      var arr;
      for (var i = 0; i < arrPara.length; i++) {
        arr = arrPara[i].split("=");
        if (arr != null && arr[0] == "code") {
          return arr[1];
        }
      }
      return null;
    } else {
      return null;
    }
  }

  //获取病人id
  getbrid() {
    var url = document.location.toString();
    var arrObj = url.split("?");
    if (arrObj.length > 1) {
      var arrPara = arrObj[1].split("&");
      var arr;
      for (var i = 0; i < arrPara.length; i++) {
        arr = arrPara[i].split("=");
        if (arr != null && arr[0] == "BRID") {
          sessionStorage.setItem('brid', arr[1]);
          this.getscanname();
          return arr[1];
        }
      }
      return null;
    } else {
      return null;
    }
  }
  //获取病人姓名
  getscanname() {
    var url = document.location.toString();
    var arrObj = url.split("?");
    if (arrObj.length > 1) {
      var arrPara = arrObj[1].split("&");
      var arr;
      for (var i = 0; i < arrPara.length; i++) {
        arr = arrPara[i].split("=");
        if (arr != null && arr[0] == "NAME") {
          sessionStorage.setItem('scanname', decodeURIComponent(arr[1]));
          //this.router.navigate(['/outpatientpay/scanoutpatientpay']);
          return arr[1];
        }
      }
      return null;
    } else {
      return null;
    }
  }

   //获取门诊号
   getpatientId() {
    var url = document.location.toString();
    var arrObj = url.split("?");
    if (arrObj.length > 1) {
      var arrPara = arrObj[1].split("&");
      var arr;
      for (var i = 0; i < arrPara.length; i++) {
        arr = arrPara[i].split("=");
        if (arr != null && arr[0] == "NAME") {
          sessionStorage.setItem('scanname', decodeURIComponent(arr[1]));
          //this.router.navigate(['/outpatientpay/scanoutpatientpay']);
          return arr[1];
        }
      }
      return null;
    } else {
      return null;
    }
  }
}
